package org.misty.redis.client;

import java.util.List;

/**
 * @author Misty on 2020-05-30
 */
public interface RedisClient {
    <T> T eval(String script, Class<T> resultType, List<String> keys, String... args);

    String getHost();

    int getPort();

    static RedisClient create(Object redis) {
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.JedisPool");
            if (cls == redis.getClass()) {
                return JedisPoolAdapter.create(redis);
            }
        } catch (ClassNotFoundException ignore) {
        }
        try {
            Class<?> cls = Class.forName("org.springframework.data.redis.core.RedisTemplate");
            if (cls == redis.getClass() || cls.isAssignableFrom(redis.getClass())) {
                return RedisTemplateAdapter.create(redis);
            }
        } catch (ClassNotFoundException ignore) {
        }
        throw new IllegalArgumentException("只能通过JedisPool/RedisTemplate的实例创建");
    }
}
